#!/usr/bin/env python
# -*- coding: cp936 -*-
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os

import logging
logging.getLogger().setLevel(logging.INFO)
#logging.error("++++++++++++ 1 cwd = %s, path = %s" % (os.getcwd(), os.environ["Path"] ))

ProjectPath = os.path.dirname(os.getcwd())
sys.path.insert(0, ProjectPath)
sys.path.insert(0, os.path.join(ProjectPath, 'common'))
sys.path.insert(0, os.path.join(ProjectPath, 'database'))

#logging.error("++++++++++++ 2 cwd = %s, path = %s" % (os.getcwd(), os.environ["Path"] ))

import wsgiref.handlers

from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import databasefunction
import commonfunction
import rss.rssdatabase as rssdatabase




class CRssHotMicroblogPage(commonfunction.CBasePage):
    def get(self):
        Language = self.GetTheSessionLanguage()
        TimeDelta = self.GetTheSessionTimeDelta()
        
        strRet = rssdatabase.GetHotMicroblogRss(Language, TimeDelta)

        self.response.headers["Content-Type"] = "xml"
        self.response.out.write(strRet)


class CRssNewMicroblogPage(commonfunction.CBasePage):
    def get(self):
        Language = self.GetTheSessionLanguage()
        TimeDelta = self.GetTheSessionTimeDelta()
        
        strRet = rssdatabase.GetNewMicroblogRss(Language, TimeDelta)

        self.response.headers["Content-Type"] = "xml"
        self.response.out.write(strRet)

class CRssHotCollectPage(commonfunction.CBasePage):
    def get(self):
        Language = self.GetTheSessionLanguage()
        TimeDelta = self.GetTheSessionTimeDelta()
        
        strRet = rssdatabase.GetHotCollectRss(Language, TimeDelta)

        self.response.headers["Content-Type"] = "xml"
        self.response.out.write(strRet)


class CRssNewCollectPage(commonfunction.CBasePage):
    def get(self):
        Language = self.GetTheSessionLanguage()
        TimeDelta = self.GetTheSessionTimeDelta()
        
        strRet = rssdatabase.GetNewCollectRss(Language, TimeDelta)

        self.response.headers["Content-Type"] = "xml"
        self.response.out.write(strRet)

class CRssHotLikePage(commonfunction.CBasePage):
    def get(self):
        Language = self.GetTheSessionLanguage()
        TimeDelta = self.GetTheSessionTimeDelta()
        
        strRet = rssdatabase.GetHotLikeRss(Language, TimeDelta)

        self.response.headers["Content-Type"] = "xml"
        self.response.out.write(strRet)

class CRssNewLikePage(commonfunction.CBasePage):
    def get(self):
        Language = self.GetTheSessionLanguage()
        TimeDelta = self.GetTheSessionTimeDelta()
        
        strRet = rssdatabase.GetNewLikeRss(Language, TimeDelta)

        self.response.headers["Content-Type"] = "xml"
        self.response.out.write(strRet)


class CRssTianYaTopPage(commonfunction.CBasePage):
    def get(self):
        self.response.headers["Content-Type"] = "xml"
        self.response.out.write( rssdatabase.GetTianYaTopNewsRSS() )



class CRssTianYaHotPage(commonfunction.CBasePage):
    def get(self):
        self.response.headers["Content-Type"] = "xml"
        self.response.out.write( rssdatabase.GetTianYaHotNewsRSS() )





class CRssResetRPCPage(commonfunction.CBasePage):
    def get(self):
        RssName = self.request.get('rssname').strip().lower()
#        logging.error("----------CCheckUserRPCPage 1 NickName = %s" % (NickName))
#        NickName = simplejson.loads(NickName)
#        logging.error("----------CCheckUserRPCPage 2 NickName = %s" % (NickName))

        Ret = rssdatabase.ResetRssCatch(RssName)
        JsonStr = simplejson.dumps(Ret)
        self.response.out.write(JsonStr)
        

def main():
    application = webapp.WSGIApplication([
                                    ('/rss_hot_microblog.xml', CRssHotMicroblogPage),
                                    ('/rss_new_microblog.xml', CRssNewMicroblogPage),
                                    ('/rss_hot_collect.xml', CRssHotCollectPage),
                                    ('/rss_new_collect.xml', CRssNewCollectPage),
                                    ('/rss_hot_like.xml', CRssHotLikePage),
                                    ('/rss_new_like.xml', CRssNewLikePage),
                                    ('/rss_tianya_top.xml', CRssTianYaTopPage),
                                    ('/rss_tianya_hot.xml', CRssTianYaHotPage),
                                    ('/rss_reset_rpc', CRssResetRPCPage),
                                    ],
                                    debug=True)
    
    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()


